import 'package:flutter/material.dart';
import 'package:flutter_model_package/CourseClass.dart';
import 'package:flutter_model_package/Employee.dart';
import 'package:flutter_model_package/Member.dart';
import 'package:flutter_model_package/parse_base.dart';
import 'package:flutter_model_package/scholar.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

class CourseEnrollment extends MSParseBase {
  CourseEnrollment() : super("CourseEnrollment");

  CourseEnrollment.cloe() : this();

  @override
  CourseEnrollment clone(Map<String, dynamic> map) =>
      CourseEnrollment.cloe()..fromJson(map);

  static Future<List<CourseEnrollment>> getMyCourse(Scholar student) async {
    var query = QueryBuilder(CourseEnrollment())
      ..whereEqualTo("student", student)
      ..includeObject(['class.course', 'class.branch']);
    var rep = await query.query();
    if (rep.results != null && rep.success) {
      return rep.results.map((e) => e as CourseEnrollment).toList();
    }
    return List.empty();
  }

  static Future<CourseEnrollment> forEnrollmentById(String id) async {
    var query = QueryBuilder(CourseEnrollment())
      ..whereEqualTo("objectId", id)
      ..includeObject(['class.course', 'class.teacher', 'class.branch']);
    var rep = await query.query();
    return rep.results.first;
  }

  ///
  static Future<List<CourseEnrollment>> forTeaClass(
      CourseClass courseClass, Employee employee) async {
    var query = QueryBuilder(CourseEnrollment())
      ..whereEqualTo("class", courseClass)
      ..whereEqualTo("handler", employee)
      ..includeObject(["student"]);
    var rep = await query.query();
    if(rep.results!=null&&rep.success){
      return rep.results.map((e) => e as CourseEnrollment).toList();
    }else {
      return List.empty();
    }
  }
  /// 检查是否已经报读
  static Future<bool> getSignUp(CourseClass courseClass, String memId) async {
    Member member = await Member.forMemScholar(memId);
    var q = QueryBuilder(CourseEnrollment())
      ..whereEqualTo("class", courseClass)
      ..whereEqualTo("student", member["scholar"]);
    var rep = await q.query();
    if (rep.success && rep.results != null) {
      return false;
    }
    return true;
  }

  /// 添加注册信息
  static Future<ParseResponse> addScholarEnrollment(
      String memId, CourseClass courseClass,
      {String name}) async {
    Member member = new Member();
    member = await Member.forMemScholar(memId);
    CourseEnrollment enrollment = new CourseEnrollment();
    if (member["scholar"] != null) {
      enrollment.set("student", member['scholar']);
      enrollment.set('joinAt', DateTime.now());
      enrollment.set('class', courseClass);
      var rep = await enrollment.save();
      return rep;
    } else {
      Scholar scholar = await Scholar.addNewScholar(name, member);
      member.set("scholar", scholar);
      await member.save();
      enrollment.set("student", scholar);
      enrollment.set('joinAt', DateTime.now());
      enrollment.set('class', courseClass);
      var rep = await enrollment.save();
      return rep;
    }
  }
}
